﻿// @brief
// @file IDimGroup.cs
// @date 2024年08月10日
// @author doublecat
// 
// Copyright (C) &lt;2024&gt;  &lt;doublecat&gt;
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

using System.Text;

namespace doublecat.mcu.svd;

public interface IDimGroup {
    /// <summary>
    /// Defines the number of elements in an array or list.
    /// </summary>
    public string? Dim { get; set; }

    /// <summary>
    /// Specify the address increment between two neighboring array or list members in the address map.	
    /// </summary>
    public string? DimIncrement { get; set; }

    /// <summary>
    /// Specify the strings that substitue the placeholder %s within &lt;name&gt; and &lt;displayName&gt;. 
    /// </summary>
    public string? DimIndex { get; set; }

    /// <summary>
    /// Specify the name of the C-type structure.
    /// </summary>
    public string? DimName { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public DimArrayIndex? ArrayIndex { get; set; }
}

public class DimArrayIndex {
    public EnumeratedValue? Value          { get; set; }
    public string?          HeaderEnumName { get; set; }
}

public static class DimGroupHelper {
    /// <summary>
    /// 生成列表
    /// </summary>
    /// <param name="dim">dimElementGroup</param>
    /// <param name="name">元素名称</param>
    /// <returns>如果name不包含%s,则返回一个长度为0的列表</returns>
    public static string GenerateList(this IDimGroup dim, string name) {
        StringBuilder sb = new StringBuilder();

        // 判断name是否包含一个数组格式[%s]
        if (name.Contains("[%s]")) {
            string replace = name.Replace("%s", dim.Dim);
            return replace;
        }
        // 检查dimIndex是否为空
        if (!string.IsNullOrEmpty(dim.DimIndex)) {
            // 逗号列表
            string[] strLists = dim.DimIndex.Split(',');
            foreach (string s in strLists) {
                // 检查s是否为字母或者是一个中间以-分隔的列表
                if (s.Contains('-')) {
                    string[] strLists1 = s.Split('-');
                    // 从'-'左端到右端循环生成每个替代字符串!
                    for (int i = int.Parse(strLists1[0]); i <= int.Parse(strLists1[1]); i++) {
                        sb.Append($"{name.Replace("%s", i.ToString())},");
                    }
                } else {
                    // 否则，替换字符串%s为s
                    sb.Append($"{name.Replace("%s", s)}");
                }
            }
        }
        return sb.ToString();
    }
}
